package com.shengcheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shengcheng.entity.BaseAttribute;
import com.shengcheng.entity.BaseCategory1;
import com.shengcheng.entity.BaseCategory3;
import com.shengcheng.entity.BaseCategoryView;
import com.shengcheng.login.TingShuLogin;
import com.shengcheng.mapper.BaseAttributeMapper;
import com.shengcheng.result.RetVal;
import com.shengcheng.service.BaseAttributeService;
import com.shengcheng.service.BaseCategory1Service;
import com.shengcheng.service.BaseCategory3Service;
import com.shengcheng.service.BaseCategoryViewService;
import com.shengcheng.util.AuthContextHolder;
import com.shengcheng.vo.CategoryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 一级分类表 前端控制器
 * </p>
 *
 * @author bch
 * @since 2024-06-25
 */
@Tag(name = "分类管理")
@RestController
@RequestMapping("/api/album/category")
public class CategoryController {

    @Autowired
    private BaseCategoryViewService baseCategoryViewService;

    @Autowired
    private BaseAttributeMapper propertyKeyMapper;

    @TingShuLogin
    @Operation(summary = "获取全部分类信息")
    @GetMapping("getAllCategoryList")
    public RetVal getAllCategoryList() {
        List<CategoryVo> categoryVoList = baseCategoryViewService.getAllCategoryList();
        return RetVal.ok(categoryVoList);
    }

    @Operation(summary = "根据一级分类Id查询分类属性信息")
    @GetMapping("getPropertyByCategory1Id/{category1Id}")
    public RetVal getPropertyByCategory1Id(@PathVariable Long category1Id) {
        List<BaseAttribute> categoryPropertyList = propertyKeyMapper.getPropertyByCategory1Id(category1Id);
        return RetVal.ok(categoryPropertyList);
    }


    /**
     * 以下为搜索模块
     * @param
     * @return
     */
    @Operation(summary = "通过三级分类id查询分类信息3")
    @GetMapping("getCategoryVive/{category3Id}")
    public BaseCategoryView getCategoryVive(@PathVariable Long category3Id) {
        BaseCategoryView categoryVive = baseCategoryViewService.getById(category3Id);
        return categoryVive;
    }

    @Autowired
    private BaseCategory3Service category3Service;

    @Operation(summary = "通过一级分类id获得三级分类列表")
    @GetMapping("getCategory3ListByCategory1Id/{category1Id}")
    public RetVal<List<BaseCategory3>> getCategory3ListByCategory1Id(@PathVariable Long category1Id) {
        List<BaseCategory3> category3List = category3Service.getCategory3ListByCategory1Id(category1Id);
        return RetVal.ok(category3List);
    }


    @Operation(summary = "通过一级分类id获取全被分类信息")
    @GetMapping("getCategoryByCategory1Id/{category1Id}")
    public RetVal getCategoryByCategory1Id(@PathVariable Long category1Id) {
        List<CategoryVo> categoryVoList = baseCategoryViewService.getCategoryByCategory1Id(category1Id);
        if(!CollectionUtils.isEmpty(categoryVoList)){
            return RetVal.ok(categoryVoList.get(0));
        }
        return RetVal.ok();
    }

    @Autowired
    private BaseCategory1Service category1Service;
    @Operation(summary = "查询一级分类")
    @GetMapping("getCategory1")
    public RetVal<List<BaseCategory1>> getCategory1() {
        LambdaQueryWrapper<BaseCategory1> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(BaseCategory1::getOrderNum);
        List<BaseCategory1> category1List = category1Service.list(wrapper);
        return RetVal.ok(category1List);
    }
}
